﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

/*      DONE: constructor system by special file;
 *      TODO: constructor system for ChromosomeTable
 *          a) visual form;           
*/


namespace ClassLibrary
{
    /* class intented to save data about one real chromosome */
    public class Chromosome
    {
        public int pare_id { set; get; }

        public Individual individual { set; get; }


        public int amount_locuses { set; get; }
        public Locus[] massive_locus;


        /*  constructor # 1
         *  Args: individual, id,
         *        alleles
        */
        public Chromosome(Individual individ, int id_choromosome_pare, int[] alleles)
        {
            individual = individ;
            pare_id = id_choromosome_pare;

            ChromosomeType chromosome_type = individual.population.species.sp_chromosome_table.table[pare_id];
            amount_locuses = chromosome_type.amount_locuses;
            massive_locus = new Locus[amount_locuses];

            Dictionary<string,int[]> chomosome_gene_link = individual.population.species.sp_chomosome_gene_link.link;

            for (int i = 0; i < chromosome_type.amount_locuses; i++)
            {
                int locus_id = i;
                massive_locus[i] = new Locus(this, locus_id, chomosome_gene_link[pare_id.ToString()][i], alleles[i]);
            }
        }
       

        /*
         * Method
         * Args: id of the locus
         * Returns: id of allele in the locus
        */
        public int getAlleleIdByLocusId(int locus_id)
        {
            return massive_locus[locus_id].getAllele();
        }

    }


    /* Description of chromosome structure */
    public class ChromosomeType
    {
        public int id { set; get; }
        public string name { set; get; }
        public int amount_locuses { set; get; }
        public int[] allele_type_id;
    }


    /* Chromosome table */
    public class ChromosomeTable
    {
        public string path { set; get; }
        public string name { set; get; }
        public int chromosome_pairs_amount;
        public ChromosomeType[] table;

        /* Constructor
         * Args: path to the file where structure of chromosomes is described
         * Do: creates massive for data of different chromosome categories
        */
        public ChromosomeTable(string path_to_chromosome_table)
        {
            path = path_to_chromosome_table;

            // open XML file            
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(File.ReadAllText(path_to_chromosome_table));

            // reading chromosome_table's attributes
            name = doc.DocumentElement.GetAttribute("Name");
            chromosome_pairs_amount = Convert.ToInt32(doc.DocumentElement.GetAttribute("ChromosomePairsAmount"));
            

            // reading all data about chromosomes
            if (chromosome_pairs_amount != 0)
            {
                // reading "chromosomes" node
                table = new ChromosomeType[chromosome_pairs_amount];                
                int i=-1;
                foreach (XmlNode x in doc.GetElementsByTagName("chromosome"))
                {
                    i++;
                    table[i] = new ChromosomeType();
                    table[i].id = Convert.ToInt32(x.Attributes[0].Value);
                    table[i].name = x.Attributes[1].Value;
                    table[i].amount_locuses = Convert.ToInt32(x.Attributes[2].Value);
                }                
            }          
        }


        public ChromosomeType getChromosomePairType(int chromosome_pare_type_id)
        {
            return table[chromosome_pare_type_id];
        }
    }
}